package com.sinosoft.ptas.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.ptas.dao.AnnualTargetMapper;
import com.sinosoft.ptas.dao.AuditEntityMapper;
import com.sinosoft.ptas.dao.IndexMapper;
import com.sinosoft.ptas.dao.MapEntityMapper;
import com.sinosoft.ptas.dao.ReportEntityMapper;
import com.sinosoft.ptas.dao.TargetsOfImplPhaseMapper;
import com.sinosoft.ptas.entity.AnnualTargetEntity;
import com.sinosoft.ptas.entity.AuditEntity;
import com.sinosoft.ptas.entity.IndexEntity;
import com.sinosoft.ptas.entity.MapEntity;
import com.sinosoft.ptas.entity.ReportEntity;
import com.sinosoft.ptas.entity.TargetsOfImplPhaseEntity;
import com.sinosoft.ptas.service.AuditUpdateService;
import com.sinosoft.ptas.system.commom.AuditConstant;

/**
 * 审核表服务接口
 * @author liufupeng
 *
 */
@Validated
public class AuditUpdateServiceImpl implements AuditUpdateService {
	
	ReportEntityMapper reportMapper;
	AuditEntityMapper auditMapper;
	TargetsOfImplPhaseMapper targetPhaseMapper;
	IndexMapper indexMapper;
	AnnualTargetMapper annualTargetMapper;
	MapEntityMapper mapEntityMapper;
	
	
	
	public void setMapEntityMapper(MapEntityMapper mapEntityMapper) {
		this.mapEntityMapper = mapEntityMapper;
	}


	public void setTargetPhaseMapper(TargetsOfImplPhaseMapper targetPhaseMapper) {
		this.targetPhaseMapper = targetPhaseMapper;
	}


	public void setIndexMapper(IndexMapper indexMapper) {
		this.indexMapper = indexMapper;
	}


	public void setAnnualTargetMapper(AnnualTargetMapper annualTargetMapper) {
		this.annualTargetMapper = annualTargetMapper;
	}


	public void setReportMapper(ReportEntityMapper reportMapper) {
		this.reportMapper = reportMapper;
	}


	public void setAuditMapper(AuditEntityMapper auditMapper) {
		this.auditMapper = auditMapper;
	}

     @Transactional(rollbackFor=Exception.class)
	public void AuditUpdateAffair( AuditEntity auditEntity){
		if(StringUtil.isBlank(auditEntity.getId())){
			String id=UUID.randomUUID().toString();
			auditEntity.setId(id);
			auditMapper.createAudit(auditEntity);
		}
		auditMapper.updateAudit(auditEntity);
		
		String reportId=auditEntity.getReportId();//得到审核表中的reportId
		ReportEntity reportEntity=reportMapper.retrieveReportById(reportId);
		
		ReportEntity newReport = reportEntity.oldToNew();//将一个旧的申报信息转换为新的申报信息
		MapEntity mapEntity=new MapEntity();
		
		String comprehensiveRating=auditEntity.getComprehensiveRating();
		if(comprehensiveRating.equals(AuditConstant.AUDIT_MIDDLE)){//如果审核表中的综合评定等级为中，则对应的申报表中的项目状态更新为建议修改
			reportEntity.setAuditResult(AuditConstant.AUDIT_MIDDLE);//更新审核结果
			reportEntity.setProjectStatus(AuditConstant.AUDIT_REVIXE);//更新申报状态
			//设置地图信息
			mapEntity.setDivisionCode(reportEntity.getDivisionCode());
			mapEntity.setAuditResult(AuditConstant.AUDIT_MIDDLE);
			mapEntity.setProjectStatue(AuditConstant.AUDIT_SAVE);
			batchToCreateTargetPhase(newReport, reportId);//新创建一个申报信息
			
		}
		if(comprehensiveRating.equals(AuditConstant.AUDIT_BAD)){//如果审核表中的综合评定等级为差，则对应的申报表中的项目状态更新为建议修改
			reportEntity.setAuditResult(AuditConstant.AUDIT_BAD);//更新审核结果
			reportEntity.setProjectStatus(AuditConstant.AUDIT_REVIXE);//更新申报状态
			//设置地图信息
			mapEntity.setDivisionCode(reportEntity.getDivisionCode());
			mapEntity.setAuditResult(AuditConstant.AUDIT_BAD);
			mapEntity.setProjectStatue(AuditConstant.AUDIT_SAVE);
			batchToCreateTargetPhase(newReport, reportId);//新创建一个申报信息
		}
		if(comprehensiveRating.equals(AuditConstant.AUDIT_BEST)||comprehensiveRating.equals(AuditConstant.AUDIT_GOOD)){
			reportEntity.setAuditResult(comprehensiveRating);
			reportEntity.setProjectStatus(AuditConstant.AUDIT_FINISH);
			//设置地图信息
			mapEntity.setDivisionCode(reportEntity.getDivisionCode());
			mapEntity.setAuditResult(comprehensiveRating);
			mapEntity.setProjectStatue(AuditConstant.AUDIT_FINISH);
		}
		mapEntityMapper.updateMapEntity(mapEntity);//更新地图信息
		reportMapper.updateReport(reportEntity);//更新原有的申报表
		
	}
	/**
	 * 新生成一条信息的申报信息
	 * @param newReport 新的申报信息
	 * @param reportId 审核表中的申报信息id
	 */
     @Transactional(rollbackFor=Exception.class)
	private void batchToCreateTargetPhase(ReportEntity newReport,String reportId){
		List<ReportEntity> reportEntitys = reportMapper.retrieveReportByProvinceCodeAndYear(newReport.getDivisionCode(),newReport.getYear());
		String reportId1=UUID.randomUUID().toString();//生成一个新的UUID用来做新申报信息的主键
		newReport.setAuditResult(AuditConstant.AUDIT_NOT_EVALUATED);
		newReport.setProjectStatus(AuditConstant.AUDIT_SAVE);
		newReport.setId(reportId1);
		newReport.setCommitTime(null);
		newReport.setTimesOfReport(reportEntitys.get(0).getTimesOfReport()+1);
		
		List<TargetsOfImplPhaseEntity> targetPhases = targetPhaseMapper.retrieveTargetsPhaseByReportIdForDefault(reportId);
		List<IndexEntity> indexs=new ArrayList<IndexEntity>();
		List<AnnualTargetEntity> annualTargets=new ArrayList<AnnualTargetEntity>();
		for(int i=0;i<targetPhases.size();i++){
			String id=UUID.randomUUID().toString();
			targetPhases.get(i).setId(id);
			targetPhases.get(i).setReportId(reportId1);
			if(targetPhases.get(i).getIndexEntityList().size()>0){//判断指标是否是空集合  排除索引越界异常
				/*给当前的实施期目标下的指标集合   在原有的值的基础上更新值   在放到一个新的集合中，创建一个新的指标集合*/
				for(int j=0;j<targetPhases.get(i).getIndexEntityList().size();j++){
				targetPhases.get(i).getIndexEntityList().get(j).setId(UUID.randomUUID().toString());//替换原有的指标编号
				targetPhases.get(i).getIndexEntityList().get(j).setTargetsOfImplPhase(id);//替换原有的指标的实施期目标的编号
				indexs.add(targetPhases.get(i).getIndexEntityList().get(j));
			}
			}
			if(targetPhases.get(i).getAnnualTargetDatalist().size()>0){//判断年度目标是否是空集合   排除索引越界异常
				/*给当前的实施期目标下的年度目标集合   在原有的值的基础上更新值   在放到一个新的集合中，创建一个新的年度目标集合*/
			for(int k=0;k<targetPhases.get(i).getAnnualTargetDatalist().size();k++){
					targetPhases.get(i).getAnnualTargetDatalist().get(k).setId(UUID.randomUUID().toString());//替换原有的年度目标编号
					targetPhases.get(i).getAnnualTargetDatalist().get(k).setTargetsOfImplPhase(id);//替换原有的年度目标的实施期目标的编号
					annualTargets.add(targetPhases.get(i).getAnnualTargetDatalist().get(k));
				}
			}
		}
		reportMapper.createReport(newReport);//创建新的申报表
		targetPhaseMapper.createTargetsOfImplPhases(targetPhases);
		indexMapper.createIndexs(indexs);
		if(annualTargets.size()>0){//如果年度目标为空则不进行批量添加
			annualTargetMapper.createAnnualTargets(annualTargets);
		}
	}
}
